// component/detailList.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    chooseId:{
      type:Number,
      value:'',
      observer:function(newval){
        this.data.id = newval
      }
    },
    ListData:{
      type:Object,
      value:{},
      observer:function(newval){
        //我的帖子
        if ((this.properties.chooseId == 0)) {
          //获取发帖者信息
         this.showArticles(newval)
        }else if(this.properties.chooseId == 1){
          this.showComments(newval)
        }else if(this.properties.chooseId == 2){
          this.showStores(newval)
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    id:'',
    authorInfo:{},
    articleInfo:{},
    articleId:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toReadArticle(){
      console.log(111);
      let id = this.data.articleId;
      console.log(id);
      wx.navigateTo({
        url:`../../pages/reading/reading?id=${id}`,
      })
    },
    showArticles(newval){
      wx.cloud.database().collection('users').where({
        userId:newval.authorId
      }).get().then(res=>{
       //更新视图
        this.setData({
          authorInfo:res.data[0],
          articleId:newval._id,
          articleInfo:newval
        })
      })
    },
    showComments(newval){
      wx.cloud.database().collection('users').where({
        openId:newval.commentUserId
      }).get().then(res=>{
        this.setData({
          authorInfo:res.data[0]
        })
      })
      wx.cloud.database().collection('articles').where({
        _id:newval.articleId
      }).get().then(res=>{
        this.setData({
          articleInfo:res.data[0],
          articleId:res.data[0]._id
        })
      })
    },
    showStores(newval){
        this.setData({
          authorInfo:newval.articletWithuser[0],
          articleId:newval._id,
          articleInfo:newval
        })
      
    }
  }
})
